package admin;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import test.CryptoUtils;
import test.LocalProperties;


public class LoginDetails {

    String PROPERTIES_FILE;
    String ENCRYPTION_METHOD;
    String KEY_FILE;
    SERVER_TYPE Selected_Server_Profile;
    String connectedServerType;
    String user_name, server_name, password;
    int port;

    public LoginDetails(LocalProperties applicaitonProfile, SERVER_TYPE Server_Profile, LOGINMETHOD method) {

	this.ENCRYPTION_METHOD=applicaitonProfile.getProperty("ENCRYPTION_METHOD");
	this.KEY_FILE= applicaitonProfile.getProperty("KEY_FILE");
	this.Selected_Server_Profile = Server_Profile;
	this.PROPERTIES_FILE = Selected_Server_Profile.getProfile();

	if (method.equals(LOGINMETHOD.FromPROPERTIES)) {
	    setConnString_fromProperties();
	} else {
	    setConnString_fromUserInput();
	}
	System.out.print("Selected PROFILE " + this.Selected_Server_Profile.toString());
    }

    private void setConnString_fromProperties() {
	LocalProperties serverProperty = new LocalProperties(PROPERTIES_FILE);
	user_name = serverProperty.getProperty("user_name");
	server_name = serverProperty.getProperty("server");
	port = Integer.parseInt(serverProperty.getProperty("port"));
	CryptoUtils myCryptoUtils = new CryptoUtils(this.ENCRYPTION_METHOD);
	password = myCryptoUtils.GetCleanPassword(this.PROPERTIES_FILE, KEY_FILE);
    }

    private void setConnString_fromUserInput() {
	LocalProperties serverProperty = new LocalProperties(PROPERTIES_FILE);
	user_name = serverProperty.getProperty("user_name");
	server_name = serverProperty.getProperty("server");
	port = Integer.parseInt(serverProperty.getProperty("port"));

	BufferedReader confirm = new BufferedReader(new InputStreamReader(System.in));
	try {
	    String answer = null;
	    answer = confirm.readLine();

	    if (answer.equals(server_name)) {
		System.out.print("\n  YOU ARE TRYING TO LOGIN TO " + Selected_Server_Profile.toString() + "\n");
	    } else {
		System.out.print("\n  WRONG SERVER NAME \n EXIT!");
		System.exit(1);
	    }
	} catch (IOException e) {
	    System.exit(1);
	}

	password = serverProperty.getProperty("password");
	if (password.length() < 3) {
	    System.out.print("Enter your password: ");
	    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    try {
		password = br.readLine();
	    } catch (IOException e) {
		System.exit(1);
	    }
	}
    }

    public void reset_key() {
	CryptoUtils myCryptoUtils = new CryptoUtils(ENCRYPTION_METHOD);

	for(SERVER_TYPE g : SERVER_TYPE.values()){
	    System.out.println(g);
	    myCryptoUtils.myGenerator(g.getProfile(), KEY_FILE);
	}
	System.out.println("Completed 'Password Reset'");
    }

    public String getPassword() {
	return password;
    }

    public int getPort() {
	return port;
    }

    public String getServerName() {
	return server_name;
    }

    public String getUserName() {
	return user_name;
    }
}
